#!/bin/bash


validate_ip() {
  local ip=$1
  local ip_pattern='^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'

  if [[ $ip =~ $ip_pattern ]]; then
    echo "Valid IP address: $ip"
  else
    echo "Invalid IP address: $ip"
    exit 1
  fi
}


while [[ $# -gt 0 ]]; do
  case "$1" in
    -h|--hostname)
      if [[ -n "$2" ]]; then
        host="$2"
        shift 2
      else
        echo "Missing argument for -h option."
        exit 1
      fi
      ;;
    --ip)
      if [[ -n "$2" ]]; then
        validate_ip "$2"
        ip="$2"
        shift 2
      else
        echo "Missing argument for --ip option."
        exit 1
      fi
      ;;
    *)
      echo "Invalid option: $1"
      exit 1
      ;;
  esac
done

echo "Host: ${host:-"master"}"
echo "IP: ${ip:-"10.0.0.11"}"

function cancel() {
  echo -e "\e[31mSince you have already installed k8s, installing it again will reset the previous installation\e[0m"
  while true; do
    read -rp "Are you sure you want to proceed? (y/n): " choice
    case "$choice" in
      [Yy])
        echo "开始重置k8s....."
        echo "chongzhi"
        isgreen "重置k8s"
        break
        ;;
      [Nn])
        echo "Operation cancelled."
        exit 0
        ;;
      *)
        echo "Invalid input. Please enter 'y' to continue or 'n' to exit."
        ;;
    esac
  done
}